
// 判断是否登录
function checkLogin() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/api/get-current-user.json");
    xhr.onload = function() {
        var r = JSON.parse(xhr.responseText);
        if (r.currentUser) {
            main(r.currentUser);
        } else {
            alert("必须登录后操作");
            window.location = "/login.html";
        }
    }
    xhr.send();
}
checkLogin();   //调用函数

function main(currentUser) {
// 首页是登录后才能使用
    var ol = document.querySelector("#content");
    var textarea = document.querySelector("#input");
    var sendBtn = document.querySelector("#send");

    var ws = new WebSocket("ws://127.0.0.1:8000/message");
    ws.onmessage = function (e) {
        var message = JSON.parse(e.data);
        var liHTML = `<li>
                        <div class="avatar"><img src="https://img01.sogoucdn.com/app/a/100520093/8379901cc65ba509-45c21ceb904429fc-7f23efc08ddbc10018b13ac470428e84.jpg"></div>
                        <div class="content-right">
                            <div class="nickname">${message.nickname}</div>
                            <div class="contentText">${message.content}</div>
                        </div>
                  </li>`;
        ol.innerHTML += liHTML;
    }

    sendBtn.onclick = function () {
        var content = textarea.value;
        textarea.value = '';
        ws.send(content);
    }
    textarea.onkeydown = function (e) {
        if (e.keyCode === 13 && e.ctrlKey) {
            var content = textarea.value;
            textarea.value = '';
            ws.send(content);
        }
    }

    var total = document.querySelector("#total");
    var list = document.querySelector("#list ol");
    function updateUserList() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "/api/user-list.json");
        xhr.onload = function () {
            var result = JSON.parse(xhr.responseText)
            total.innerText = `群成员${result.onlineCount} / ${result.totalCount}`;
            list.innerHTML = '';
            for (var i in result.userList) {
                var user = result.userList[i];
                var html;
                if (user.online) {
                    html = `<li>
                            <div class="avatar"> <!-- 群成员头像 -->
                                <img src="http://img.wxcha.com/m00/98/e2/1defb6b4d898a94b8832287e503a0153.jpg">
                            </div>
                            <div class="nickname">${user.nickname}</div> <!-- 群成员昵称 -->
                        </li>`;
                } else {
                    html = `<li class="offline"> <!-- 不在线 -->
                            <div class="avatar">
                                <img src="http://img.wxcha.com/m00/98/e2/1defb6b4d898a94b8832287e503a0153.jpg">
                            </div>
                            <div class="nickname">${user.nickname}</div>
                        </li>`;
                }
                list.innerHTML += html;
            }
        }
        xhr.send();
    }

    updateUserList();
    setInterval(updateUserList, 1000);
}